package com.vgooo.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.vgooo.bean.UserInfo;
/**
 * MD5工具
 * @author Administrator
 *
 */
public class MD5Utils {
	/**
	 * 获取字符串source的ＭＤ５加密后的值
	 * @param source 需要加密的字符串
	 * @return MD5_code 加密后字符串
	 */
	public static String getMD5(String source) {
		String str;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(source.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
//			System.out.println("result: " + buf.toString());// 32位的加密
//			System.out.println("result: " + buf.toString().substring(8, 24));// 16位的加密
//			return buf.toString() ;
			return str ;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取用户登录时的code
	 * @param user 用户信息
	 * @return MD5_code 
	 */
	public static String getLCode(String userName, String password){
		return getMD5(userName+getMD5(password)+"vgooologin");
		//String md5 = md5s("805015788@qq.com"+md5s("asdfghk")+"vgooologin");
	}
	
	
	/**
	 * 获取用户登录时的ucode
	 * @param userId
	 * @return MD5_code 
	 */
	public static String getUCode(String userId){
		return getMD5(userId+getMD5("vgooouserinfo"+userId));
	}
	
	
	
}
